BigDb.php.bak

<?php

namespace Tlf\BigDb\Test;

class BigDb extends \Tlf\Tester {

    protected \PDO $pdo;

    public function prepare(){
        $this->pdo = $this->getPdo();

        try {
            $this->build_database();
        } catch (\Exception $e){
            // do nothing
            throw $e;
        }
    }
    public function bdb(): \Tlf\BigDb {
        $bdb = new \Tlf\BigDb($this->pdo);
        $bdb->check_serialized_file($this->file('test/input/bigdb-sql'), 'dv');
        $bdb->load_queries($this->file('test/input/bigdb-sql'), 'dv');
        $bdb->addOrmNamespace('Tlf\\LilDb\\Test\\BigDbOrm');
        return $bdb;
    }

    public function build_database(){
        $bdb = $this->bdb();
        $stmt = $bdb->exec('dv.articles.create');
        $stmt = $bdb->exec('dv.articles.empty');
        $bdb->exec('dv.articles.insert_test_rows');
    }

    public function testBuildDatabase(){
        $this->build_database();

        // return;
        $stmt = $this->pdo->prepare("SELECT title FROM article ORDER BY `id` DESC");
        $stmt->execute();
        
        $rows = $stmt->fetchAll(\PDO::FETCH_NUM);
        $titles = [];
        foreach ($rows as $r){
            $titles[] = $r[0];
        }
        $titles = implode("-",$titles);

        $this->compare('Four, Private-Three-Two-One',$titles,true);
    }

    /**
     * Test that multiple queries execute using `BigDb::exec()`
     */ 
    public function testMultiExec(){
        $pdo = $this->pdo;
        $bdb = $this->bdb();

        // test creating, then querying. creating is a multi-query
        $bdb->exec("dv.articles.create");
        $articles = $bdb->query("SELECT * FROM article", [], 'article');;
        $titles = '';
        foreach ($articles as $article){
            $titles .= $article->title.'-';
        }
        $this->compare('One-Two-Three-Four, Private-', $titles);
        


        // test DROP then CREATE TABLE with no inserts, as a multi-query
        $bdb->exec("dv.articles.recreate");
        $articles = $bdb->query("SELECT * FROM article", [], 'article');;
        $titles = '';
        foreach ($articles as $article){
            $titles .= $article->title.'-';
        }
        $this->compare('', $titles);

    }

    public function testAccess(){
        $this->disable();

        $bdb = new \Tlf\BigDb($this->pdo);
        $bdb->check_serialized_file($this->file('test/input/bigdb-sql'), 'dv');
        $bdb->load_queries($this->file('test/input/bigdb-sql'), 'dv');
        $bdb->addOrmNamespace('Tlf\\LilDb\\Test\\BigDbOrm');

        // just returns true for everything
        $access_layer = new \Tlf\BigDb\Test\TestAccessLayer();
        $bdb->setAccessLayer($access_layer);


        

    }


    // public function

    /**
     * Just for trying out new features
     */
    public function testStuff(){
        $this->disable();

        require_once($this->file('test/input/bigdb-sql/Article.php'));
        // $bdb = \Tlf\BigDb::mysql('reed', trim(file_get_contents($this->file('/mysqlpass.txt'))), 'decatur_vote');
        $bdb = $this->bdb();
        // $query = $bdb->get_query('dv.articles.create');
        // $pdo = $bdb->get_pdo();
        // $pdo->exec($query);
        //
        // $bdb->exec('dv.articles.create');
        $bdb->exec('dv.articles.empty');

// echo "\n\n\n-----------\n\n";
        // echo "\nQuery:\n$query\n";
// echo "\n\n\n-----------\n\n";
        $public_articles = $bdb->load('dv.articles.get_public', 'article');

        foreach ($public_articles as $article){
            // var_dump($article->id);
//
            // var_dump($article->related_article_id);
            if ($article->related_article_id==null)continue;
            echo "\nSelf ("
                .$article->id.','
                .$article->title
                .")";
            echo "\nRelated ("
                .$article->related_article->id.','
                .$article->relatedTitle
                .")";
        }

    }


}